﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chessLogic
{
    public class Statistics
    {
        private string[,] dataTable;
        public string[,] DataTable
        { get { return this.dataTable; } }

        public Statistics(string[,] _dataTable)
        {
            this.dataTable = _dataTable;
        }

        public string[,] GetStatistics()
        {
            List<string> differentMoves = GetDifferentMoves();
            string[,] statistics = new string[differentMoves.Count, 5];
            for (int i = 0; i < differentMoves.Count; i++)
            {
                statistics[i, 0] = differentMoves[i];
                statistics[i, 1] = Convert.ToString(GetCountGamesWithThisMove(differentMoves[i]));
                statistics[i, 2] = Convert.ToString(GetPercent("1-0", differentMoves[i]));
                statistics[i, 3] = Convert.ToString(GetPercent("0-1", differentMoves[i]));
                statistics[i, 4] = Convert.ToString(GetPercent("1/2-1/2", differentMoves[i]));
            }
            return statistics;
        }

        private List<string> GetDifferentMoves()
        {
            List<string> differentMoves = new List<string>();
            for (int i = 0; i < dataTable.GetLength(0); i++)
            {
                if (!differentMoves.Contains(dataTable[i, 2]))
                    differentMoves.Add(dataTable[i, 2]);
            }
            return differentMoves;
        }
        private double GetPercent(string result, string thisMove)
        {
            double percent = 0.0;
            for (int i = 0; i < dataTable.GetLength(0); i++)
            {
                if (thisMove == dataTable[i, 2] && result == dataTable[i, 1])
                    percent += 1;
            }
            return (percent / GetCountGamesWithThisMove(thisMove)) * 100;
        }
        private int GetCountGamesWithThisMove(string thisMove)
        {
            int countGames = 0;
            for (int i = 0; i < dataTable.GetLength(0); i++)
            {
                if (thisMove == dataTable[i, 2])
                    countGames++;
            }
            return countGames;
        }
    }
}
